package com.wgy.designmode.action.memento;

/**
 * 备忘录模式
 * 使用场景：当需要提供一种撤销机制，允许用户回退到之前的状态时。
 */
public class MementopatternDemo {


    public static void main(String[] args) {
        Originator originator = new Originator();
        Caretaker caretaker = new Caretaker();

        // 设置初始状态并保存
        originator.setState("State #1");
        caretaker.addMemento(originator.saveStateToMemento());

        // 修改状态并保存
        originator.setState("State #2");
        caretaker.addMemento(originator.saveStateToMemento());

        // 再次修改状态
        originator.setState("State #3");
        System.out.println("Current State: " + originator.getState());

        // 恢复到第一个保存的状态
        originator.restoreStateFromMemento(caretaker.getMemento(0));
        System.out.println("First saved State: " + originator.getState());

        // 恢复到第二个保存的状态
        originator.restoreStateFromMemento(caretaker.getMemento(1));
        System.out.println("Second saved State: " + originator.getState());


    }
}
